package com.kukudexin.Test;

import com.kukudexin.server.WebSocketServer;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;

/**
 * @author: 德鑫
 * @description:
 * @since: 2021/5/17
 */
@RestController
@Component
public class DemoController {
    @GetMapping("index")
    public ResponseEntity<String> index(){
        return ResponseEntity.ok("请求成功");
    }

    @GetMapping("/page/{userId}")
    public ModelAndView page(@PathVariable("userId") String userId){
        ModelAndView mv = new ModelAndView("websocket");
        mv.addObject("userId",userId);
        return mv;
    }

    @RequestMapping("/push/{toUserId}")
    public ResponseEntity<String> pushToWeb(String message, @PathVariable String toUserId) throws IOException {
        WebSocketServer.sendInfo(message,toUserId);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }

}
